package com.tools;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;

public class SensitiveWordInit {
    public static Map sensitiveWordMap = null;


    /**
     * 加载敏感词库
     *
     * @return
     * @throws Exception
     */
    public Set<String> LoadSetitiveWord() throws Exception {
        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;
        Set<String> set = new HashSet<String>();
        try {
            inputStream = getClass().getClassLoader().getResourceAsStream("words.txt");

            inputStreamReader = new InputStreamReader(inputStream, "UTF-8");

            bufferedReader = new BufferedReader(inputStreamReader);

            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                set.add(str);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                bufferedReader.close();
            }
            if (inputStreamReader != null) {
                inputStreamReader.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }
        }
        return set;
    }

    /**
     * 把敏感词加入到HashMap
     *单个字单个字加进Map，过滤掉，
     * @param keyWordSet
     * @return
     */
    public Map addSensitiveWordToHashMap(Set<String> keyWordSet){
        sensitiveWordMap  = new HashMap(keyWordSet.size());
        Map nowMap = null;
        Iterator iterator = keyWordSet.iterator();
        while(iterator.hasNext()){
            String str = (String) iterator.next();
            nowMap = sensitiveWordMap;
            Object wordMap =null;
            for(int i = 0;i<str.length();i++){
                char word = str.charAt(i);
                if(word!='，'||word!=','||word!='.'||word!='。'||word!='！'||word!='!'||word!='?'||word!='？'||word!='-'||word!='_'||word!='—'){
                     wordMap = word;
                }
                if(wordMap!=null ){
                    nowMap.put(wordMap,i);
                }else{
                    Map<String,String> newMap = new HashMap<String,String>();
                    newMap.put("isEnd","0");
                    nowMap.put(word,newMap);
                    nowMap = newMap;
                }
                if(i==str.length()-1){
                    nowMap.put("isEnd","1");
                }
            }
        }
        return sensitiveWordMap;
    }



    public Map strSensitiveWordToHashMap(Set<String> keyWordSet) {
        sensitiveWordMap = new HashMap(keyWordSet.size());
        Map nowMap = null;
        Iterator iterator = keyWordSet.iterator();
        while (iterator.hasNext()) {
            String str = (String) iterator.next();
            nowMap = sensitiveWordMap;
            if (str.length() == 0) {
                Map<String, String> newMap = new HashMap<String, String>();
                newMap.put("isEnd", "0");
                nowMap.put(null, newMap);
                nowMap = newMap;
            }
            int start = 0;
            int count = 0;
            for (int i = 0; i < str.length(); i++) {
                Object strMap = null;
                if (str.charAt(i) == '，' || i == str.length() - 1) {
                    if (i == str.length() - 1) {
                        strMap = str.substring(start);
                        nowMap.put(strMap, count);
                        nowMap.put("isEnd", "1");
                    } else {
                        strMap = str.substring(start, i);
                        nowMap.put(strMap, count);
                    }
                    start = i + 1;
                    count++;

                } else {
                    continue;
                }
            }
        }
        return sensitiveWordMap;
    }
}